'''

Case Type： pid文件
Case Name： pid文件已存在，启动数据库
Create At:  2020/06/24
Owner    :  n@ningyali
Descption:  1.查看pid文件 2.获取pid 3.根据pid杀进程 4.启动数据库

'''

import os
import sys
import unittest
import time
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common


class PidExistsStart(unittest.TestCase):
    sh = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case078.py start------------')
        self.pid_file_path = os.path.join(macro.DB_INSTANCE_PATH, macro.PID_FILE_NAME)
        self.rootNode = Node()
        self.dbUserNode = Node(node='dbuser')
        self.com = Common()

    def test_pid_exists_start_db(self):
        # 查看pid文件
        view_pid_cmd = f'cat {self.pid_file_path}'
        msg = self.dbUserNode.sh(view_pid_cmd).result()
        self.log.info(msg)
        self.assertGreater(len(msg), 0)

        # 获取pid，根据pid杀进程
        msg = self.com.kill_pid(self.rootNode, 9)
        self.assertEqual(msg, '')

        # 启动数据库
        is_started = self.sh.startDbCluster()
        self.assertTrue(is_started)

    def tearDown(self):
        self.log.info('-------------Opengauss_Reliability_Dbsys_Case078 end---------------')
